﻿using Enums;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using XF.Common;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmSaleStatementList : Form
    {
        private readonly XF.BLL.Base_Customer bllCustomer = new XF.BLL.Base_Customer();
        private readonly XF.BLL.Base_CheckOut bllCheckOut = new XF.BLL.Base_CheckOut();
        private readonly XF.BLL.Bill_Receipt bllReceipt = new XF.BLL.Bill_Receipt();
        public FrmSaleStatementList()
        {
            InitializeComponent();
        }

        private void FrmSaleStatement_Load(object sender, EventArgs e)
        {
            LoadPayType();
        }

        private void LoadPayType()
        {
            DataTable dt = GenerateData.GetPayType(false);
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem listViewItem = new ListViewItem(zDataConverter.ToString(dr["Text"]), 0);
                listViewItem.Tag = zDataConverter.ToInt(dr["Value"]);
                lvPayType.Items.Add(listViewItem);
            }
            lvPayType.Focus();
            if (lvPayType.Items.Count > 0)
            {
                lvPayType.Items[0].Selected = true;
            }
        }

        private void lvPayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCustomer();
        }

        private void LoadCustomer()
        {
            if (lvPayType.SelectedItems.Count > 0)
            {
                int iPayType = zDataConverter.ToInt(lvPayType.SelectedItems[0].Tag);
                string sWhere = " PayType = " + iPayType.ToString();
                if(!tsTbCustomer.Text.Equals(string.Empty))
                {
                    sWhere += " and (CustomerCode like '%" + tsTbCustomer.Text + "%' or CustomerName like '%" + tsTbCustomer.Text + "%') ";
                }
                List<XF.Model.Base_Customer> customers = bllCustomer.GetModelList(sWhere);
                lvCustomers.Items.Clear();
                foreach (XF.Model.Base_Customer customer in customers)
                {
                    ListViewItem listViewItem = new ListViewItem(customer.No + "-" + customer.CustomerName, 1);
                    listViewItem.Tag = customer.Id;
                    lvCustomers.Items.Add(listViewItem);
                }
            }
        }

        private void lvCustomers_DoubleClick(object sender, EventArgs e)
        {
            //MessageBox.Show(lvCustomers.SelectedItems[0].Tag.ToString());
        }

        private void tsBtnHistory_Click(object sender, EventArgs e)
        {
            if(lvCustomers.SelectedItems.Count>0)
            {
                int iCustomerId = zDataConverter.ToInt(lvCustomers.SelectedItems[0].Tag);
                XF.Model.Base_CheckOut model = bllCheckOut.GetLastModel(iCustomerId);
                if (model == null)
                {
                    QQMessageBox.Show(
                        this,
                        MessageText.CHECK_ERROR_NORECORD,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
                }
                else
                {
                    FrmSaleStatement frmSaleStatement = new FrmSaleStatement(iCustomerId);
                    frmSaleStatement.ShowDialog();
                }
            }
            else
            {
                QQMessageBox.Show(
                        this,
                        MessageText.TIP_CHOISE_CUSTOMER,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnSettlement_Click(object sender, EventArgs e)
        {
            if (lvCustomers.SelectedItems.Count > 0)
            {
                int iCustomerId = zDataConverter.ToInt(lvCustomers.SelectedItems[0].Tag);
                XF.Model.Base_CheckOut checkOut = bllCheckOut.GetLastModel(iCustomerId);
                //if (checkOut == null || checkOut.PaidDate != null)
                //{
                    FrmSaleStatementUnclean frmSaleStatementUnclean = new FrmSaleStatementUnclean(iCustomerId);
                    frmSaleStatementUnclean.ShowDialog();
                //}
                //else
                //{
                //    QQMessageBox.Show(
                //        this,
                //        MessageText.CHECK_ERROR_UNPAID,
                //        MessageText.MESSAGEBOX_CAPTION_TIP,
                //        QQMessageBoxIcon.Information,
                //        QQMessageBoxButtons.OK);
                //}
            }
            else
            {
                QQMessageBox.Show(
                        this,
                        MessageText.TIP_CHOISE_CUSTOMER,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            if (lvCustomers.SelectedItems.Count > 0)
            {
                XF.Model.Base_CheckOut model = bllCheckOut.GetLastModel(zDataConverter.ToInt(lvCustomers.SelectedItems[0].Tag, -1));
                if(model == null)
                {
                    QQMessageBox.Show(
                        this,
                        MessageText.CHECK_ERROR_NORECORD,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
                }
                else
                {
                    if(QQMessageBox.Show(
                        this,
                        string.Format(MessageText.TIP_DELETE_CHECKOUT,model.CheckOutDate.ToString("yyyy-MM-dd"),model.Arrears),
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Warning,
                        QQMessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        if (bllCheckOut.Delete(model.Id))
                        {
                            QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_REVOKE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                        }
                        else
                        {
                            QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_REVOKE,
                            MessageText.MESSAGEBOX_CAPTION_ERROR,
                            QQMessageBoxIcon.Error,
                            QQMessageBoxButtons.OK);
                        }
                    }
                }
            }
            else
            {
                QQMessageBox.Show(
                        this,
                        MessageText.TIP_CHOISE_CUSTOMER,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnRecord_Click(object sender, EventArgs e)
        {
            XF.Model.Base_CheckOut model = bllCheckOut.GetLastModel(zDataConverter.ToInt(lvCustomers.SelectedItems[0].Tag, -1));
            if (model == null)
            {
                QQMessageBox.Show(
                    this,
                    MessageText.CHECK_ERROR_NORECORD,
                    MessageText.MESSAGEBOX_CAPTION_TIP,
                    QQMessageBoxIcon.Information,
                    QQMessageBoxButtons.OK);
            }
            else
            {
                FrmDialogEntry frmDialogEntry = new FrmDialogEntry(model.Id);
                frmDialogEntry.ShowDialog();
            }
        }

        private void tsBtnQuery_Click(object sender, EventArgs e)
        {
            LoadCustomer();
        }

        private void tsBtnPay_Click(object sender, EventArgs e)
        {
            if (lvCustomers.SelectedItems.Count > 0)
            {
                XF.Model.Bill_Receipt model = new XF.Model.Bill_Receipt();
                XF.Model.Base_Customer customer = bllCustomer.GetModel(zDataConverter.ToInt(lvCustomers.SelectedItems[0].Tag));
                if (customer != null)
                {
                    model.CustomerId = customer.Id;
                    model.CustomerCode = customer.CustomerCode;
                    model.CustomerName = customer.CustomerName;
                }
                model.PaidDate = DateTime.Now;
                model.PaidAmount = 0;

                FrmDialogReceipt frmDialogReceipt = new FrmDialogReceipt();
                frmDialogReceipt.Receipt = model;
                if (frmDialogReceipt.ShowDialog() == DialogResult.OK)
                {
                    if (bllReceipt.Add(frmDialogReceipt.Receipt)>-1)
                    {
                        QQMessageBox.Show(
                                this,
                                MessageText.TIP_SUCCESS_RECEIPT,
                                MessageText.MESSAGEBOX_CAPTION_TIP,
                                QQMessageBoxIcon.OK,
                                QQMessageBoxButtons.OK);
                    }
                    else
                    {
                        QQMessageBox.Show(
                                this,
                                MessageText.TIP_ERROR_RECEIPT,
                                MessageText.MESSAGEBOX_CAPTION_TIP,
                                QQMessageBoxIcon.Information,
                                QQMessageBoxButtons.OK);
                    }

                }
            }
            else
            {
                QQMessageBox.Show(
                        this,
                        MessageText.TIP_CHOISE_CUSTOMER,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnInit_Click(object sender, EventArgs e)
        {
            if (lvCustomers.SelectedItems.Count > 0)
            {
                FrmDialogInit frmDialogInit = new FrmDialogInit(zDataConverter.ToInt(lvCustomers.SelectedItems[0].Tag));
                if(frmDialogInit.ShowDialog() == DialogResult.OK)
                {
                    if (bllCheckOut.Add(frmDialogInit.CheckOut) > -1)
                    {
                        QQMessageBox.Show(
                                    this,
                                    MessageText.TIP_SUCCESS_INIT,
                                    MessageText.MESSAGEBOX_CAPTION_TIP,
                                    QQMessageBoxIcon.OK,
                                    QQMessageBoxButtons.OK);
                    }
                    else
                    {
                        QQMessageBox.Show(
                                    this,
                                    MessageText.TIP_ERROR_INIT,
                                    MessageText.MESSAGEBOX_CAPTION_TIP,
                                    QQMessageBoxIcon.Information,
                                    QQMessageBoxButtons.OK);
                    }
                }
            }
            else
            {
                QQMessageBox.Show(
                        this,
                        MessageText.TIP_CHOISE_CUSTOMER,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
            }
        }
    }
}
